package com.example.conf;

import com.example.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
//    @Bean
//    public Docket docket() {
//        ApiInfo apiInfo = new ApiInfoBuilder()
//                .title("管家婆接口文档") //指定接口名称
//                .version("2.0") //Swagger版本
//                .description("管家婆项目接口文档") //接口文档描述
//                .build();
//        Docket docket = new Docket(DocumentationType.SWAGGER_2) //选择Swagger的版本
//                .apiInfo(apiInfo)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.example.controller")) //指定了扫描生成接口文档的接口的所在的包
//                .paths(PathSelectors.any())
//                .build();
//        return docket;
//    }
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态映射.......");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        //此时在url中出现的doc.html将不再被认为是url参数的一部分，而是作为一个html页面
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Autowired
    private JwtTokenUserInterceptor loginCheckInterceptor;
    String[] excludePatterns = new String[]{"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
            "/api", "/api-docs", "/api-docs/**", "/doc.html/**","/report/**","/user/login","/user/register","/login2","/m","/favicon.ico",
            "/v2/api-docs"};
    // 重写接口方法
    public void addInterceptors(InterceptorRegistry registry){
        log.info("开始注册自定义拦截器......");
        // 注册一个拦截器
        registry.addInterceptor(loginCheckInterceptor)
                .addPathPatterns("/**") // 指定需要拦截的请求地址
                .excludePathPatterns("/v3/api-docs/swagger-config")
                .excludePathPatterns("/v3/api-docs/**")
                .excludePathPatterns(excludePatterns);
    }

}
